package com.example.tool;

import com.example.entity.Question;
import com.example.entity.Question_Student;
import com.example.entity.Test;
import com.example.entity.User;
import com.example.mapper.StudentMapper;
import com.example.service.SparkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Component
@Slf4j
public class QuestionGenerator {
    private final SparkService sparkService;
    private final StudentMapper studentMapper;

    public QuestionGenerator(SparkService sparkService, StudentMapper studentMapper) {
        this.sparkService = sparkService;
        this.studentMapper = studentMapper;
    }

    public Map<String,List<Question>> generateQuestionsConcurrently(Test test, User user) {

        int choiceNum = test.getCnum();
        int fillNum = test.getFnum();
        int dealNum = test.getSnum();

        ExecutorService executor = Executors.newFixedThreadPool(choiceNum);

        Map<String,List<Question>> allMap = new HashMap<>();
        List<Question> choiceList = new ArrayList<>();
        List<Question> fillList = new ArrayList<>();
        List<Question> dealList = new ArrayList<>();

        //选择题
        for (int t = 1; t <= ( choiceNum < 5 ? 1:((choiceNum % 5 != 0 ? choiceNum / 5 + 1:choiceNum / 5))) ; t++) {

            try {
                Thread.sleep(500); // 每个线程启动前等待 0.3 秒
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            int num = t * 5 ;

            executor.submit(() -> {
                int createNum = 5;
                List<Question> questionList = new ArrayList<>();

                if(num > choiceNum){
                    createNum = createNum + 5 - num;
                }

                String choice_input = "知识点范围：\n" + test.getKpoint() + "\n" + "题目类型：\n" + "选择题\n" + "题目数量：\n" + createNum +"道 \n" + "学科: \n" + test.getSubject() + "\n";
                List<Question> questionChoice = sparkService.createChoiceQuestionAi(choice_input, createNum);

                for (Question question : questionChoice) {
                    question.setTid(test.getTid());
                    question.setKpoint(test.getKpoint());
                    question.setType("选择题");

                    questionList.add(question);
                }

                choiceList.addAll(questionList);
                System.out.println("线程 " + Thread.currentThread().getName() + " 生成完毕！");
            });
        }

        //填空题
        for (int t = 1; t <= (fillNum % 5 != 0 ? fillNum / 5 + 1:fillNum / 5); t++) {

            try {
                Thread.sleep(500); // 每个线程启动前等待 0.3 秒
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            int num = t * 5 ;

            executor.submit(() -> {
                int createNum = 5;
                List<Question> questionList = new ArrayList<>();

                if(num > fillNum){
                    createNum = createNum + 5 - num;
                }

                String fill_input = "知识点范围：\n" + test.getKpoint() + "\n" + "题目类型：\n" + "填空题\n" + "题目数量：\n" + createNum + "道 \n" + "学科: \n" + test.getSubject() + "\n";
                List<Question> questionFill = sparkService.createQuestionAi(fill_input, createNum);

                for (Question question : questionFill) {
                    question.setTid(test.getTid());
                    question.setKpoint(test.getKpoint());
                    question.setType("填空题");

                    questionList.add(question);
                }
                fillList.addAll(questionList);
            });
        }

        //解答题
        for (int t = 1; t <= (dealNum % 5 != 0 ? dealNum / 5 + 1:dealNum / 5) ; t++) {

            try {
                Thread.sleep(500); // 每个线程启动前等待 0.3 秒
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            int num = t * 5 ;

            executor.submit(() -> {
                int createNum = 5;
                List<Question> questionList = new ArrayList<>();

                if(num > dealNum){
                    createNum = createNum + 5 - num;
                }

                String deal_input = "知识点范围：\n" + test.getKpoint() + "\n" + "题目类型：\n" + "解答题\n" + "题目数量：\n" + createNum + "道 \n" + "学科: \n" + test.getSubject() + "\n";

                List<Question> questionDeal = sparkService.createQuestionAi(deal_input, createNum);

                for (Question question : questionDeal) {
                    question.setTid(test.getTid());
                    question.setKpoint(test.getKpoint());
                    question.setType("解答题");

                    questionList.add(question);
                }

                dealList.addAll(questionList);
            });
        }

        executor.shutdown();
        try {
            if (!executor.awaitTermination(10, TimeUnit.MINUTES)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }

        allMap.put("选择题",choiceList);
        allMap.put("解答题",dealList);
        allMap.put("填空题",fillList);

        return allMap;
    }
}
